package immobilon.livestream;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class LoLLivestreamsActivity extends Activity {
	ProgressBar loadingBar; 
	LinearLayout layout;
	ArrayList<Livestream> streamList = new ArrayList<Livestream>();
	ArrayList<TextView> textList;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        layout = (LinearLayout) this.findViewById(R.id.linearLayout2);
        loadingBar = (ProgressBar) this.findViewById(R.id.loadingBar);
        textList = new ArrayList<TextView>();
        loadingBar.setVisibility(View.VISIBLE);
        
        DownloadTask task = new DownloadTask();
        task.execute(streamList);
        
        
    }
    
    public ArrayList<Livestream> getStreams(ArrayList<Livestream> streamList) throws Exception {
        URL livestreams = new URL("http://clgaming.net/livestreams/");
        BufferedReader in = new BufferedReader(new InputStreamReader(livestreams.openStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null){
        	if(inputLine.contains("<a href=\"/live/")){
        		String url, player, description, time;
        		
        		//get the livestream's url.
        		int beginContent = inputLine.lastIndexOf("/live/");
        		int endContent = inputLine.lastIndexOf("</a>");
        		url = "<a href=\"http://www.clgaming.net"+inputLine.substring(beginContent, endContent);
        		//mTextSample.setText(Html.fromHtml(text));
        		
        		/*//get the livestream's player name.
        		beginContent = inputLine.lastIndexOf("\">")+2;
        		endContent = inputLine.lastIndexOf("</a>");
        		player = inputLine.substring(beginContent, endContent);*/
        		
        		//get the livestream's time.
        		inputLine = in.readLine();
        		beginContent = inputLine.lastIndexOf("\">")+2;
        		endContent = inputLine.lastIndexOf("</div>");
        		time = inputLine.substring(beginContent, endContent);
        		
        		//get the livestream's description.
        		inputLine = in.readLine();
        		beginContent = inputLine.lastIndexOf("\">")+2;
        		endContent = inputLine.lastIndexOf("</div>");
        		if(beginContent<=endContent) description = inputLine.substring(beginContent, endContent);
                else description = "";
        		
        		if(!time.contains("offline")){
        			Livestream stream = new Livestream(url, description, time);
        			streamList.add(stream);
        		}
        		
        	}
        }
        in.close();
        return streamList;
      }
    
    private class DownloadTask extends AsyncTask<ArrayList<Livestream>, Void, ArrayList<Livestream>>{
    	
		@Override
		protected ArrayList<Livestream> doInBackground(
				ArrayList<Livestream>... list) {
			try{
	        	LoLLivestreamsActivity.this.streamList = getStreams(list[0]);
	        }
	        catch(Exception e){
	        	Toast.makeText(LoLLivestreamsActivity.this, "Unable to get livestreams.\nIs your connection working?", Toast.LENGTH_LONG).show();
	        }
			return LoLLivestreamsActivity.this.streamList;
		}
    	
		@Override
		protected void onPostExecute(ArrayList<Livestream> streamList) {
			
	        for(int i=0;i<streamList.size();i++){
	        	TextView playerName = new TextView(LoLLivestreamsActivity.this);
	        	playerName.setText(Html.fromHtml(streamList.get(i).getHtml()));
	        	playerName.setTextSize(18f);
	        	playerName.setMovementMethod(LinkMovementMethod.getInstance());
	        	TextView description = new TextView(LoLLivestreamsActivity.this);
	        	description.setText(streamList.get(i).getDescription());
	        	TextView time = new TextView(LoLLivestreamsActivity.this);
	        	time.setText(streamList.get(i).getTime());
	        	time.setTextSize(10f);
	        	layout.addView(playerName);
	        	layout.addView(time);
	        	if(!streamList.get(i).getDescription().equals("")) layout.addView(description);
	        	
	        }
	        loadingBar.setVisibility(View.GONE);
		}
    }
}